﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Utils;
using Telerik.Web.UI;
using DCSLibrary;
using Utils.DC.Web.UI;

public partial class Admin_Module_Control_ActionForm : System.Web.UI.UserControl
{
    
    private RadGrid grd;
    /// <summary>
    /// Ten Grid
    /// </summary>
    public RadGrid Grid
    {
        get
        {
            return grd;
        }
        set
        {
            grd = value;
        }
    }

    private Label lbl;
    /// <summary>
    /// Label hien thong bao
    /// </summary>
    public Label MessageLabel
    {
        get
        {
            return lbl;
        }
        set
        {
            lbl = value;
        }
    }

    private string actionList;

    /// <summary>
    /// Ten chuoi action muon thao tac. VD: Hot;New
    /// </summary>
    public string ActionList
    {
        get
        {
            return actionList;
        }
        set
        {
            actionList = value;
        }
    }

    private string type;
    /// <summary>
    /// VD: News, hoac Product
    /// </summary>
    public string Type
    {
        get
        {
            return type;
        }
        set
        {
            type = value;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindAction();
        }
    }

    private void BindAction()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("ActionName"));
        dt.Columns.Add(new DataColumn("ActionValue"));

        actionList = actionList.ToLower();
        if (actionList.Contains("hot"))
        {
            dt.LoadDataRow(new object[] { "Thêm Tin Tức Hot", Common.ACTION_NAME.HOT }, true);
            dt.LoadDataRow(new object[] { "Bỏ Tin Tức Hot", Common.ACTION_NAME.NOT_HOT }, true);
        }

        if (actionList.Contains("typical"))
        {
            dt.LoadDataRow(new object[] { "Thêm Tin Tức Nổi Bật", Common.ACTION_NAME.TYPICAL }, true);
            dt.LoadDataRow(new object[] { "Bỏ Tin Tức Nổi Bật", Common.ACTION_NAME.NOT_TYPICAL }, true);
        }

        if (actionList.Contains("new"))
        {
            dt.LoadDataRow(new object[] { "Thêm Tin Tức Mới", Common.ACTION_NAME.NEW }, true);
            dt.LoadDataRow(new object[] { "Bỏ Tin Tức Mới", Common.ACTION_NAME.NOT_NEW }, true);
        }

        if (actionList.Contains("approved"))
        {
            dt.LoadDataRow(new object[] { "Duyệt Tin Tức", Common.ACTION_NAME.APPROVED }, true);
            dt.LoadDataRow(new object[] { "Bỏ Duyệt Tin Tức", Common.ACTION_NAME.NOT_APPROVED }, true);
        }

        cboAction.DataValueField = "ActionValue";
        cboAction.DataTextField = "ActionName";

        cboAction.DataSource = dt;
        cboAction.DataBind();
    }


    protected void Action_Click(object sender, EventArgs e)
    {
        string action = cboAction.SelectedValue;

        if (grd != null && grd.SelectedItems != null && grd.SelectedItems.Count > 0)
        {
            IEnumerator ie = grd.SelectedItems.GetEnumerator();
            GridItem item = null;

            string listID = "";

            //Duyet tat ca phan tu duoc chon, ghep thanh 1 chuoi ID cach nhau boi dau ;
            while (ie.MoveNext())
            {
                item = (GridDataItem)ie.Current;

                //Cot thu 4 la cot ID
                listID += item.Cells[3].Text + ",";
            }


            if (listID.Length > 1)
            {

                listID = listID.Substring(0, listID.Length - 1);
                try
                {
                    int result = 0;
                    if (type.ToLower() == "news")
                    {
                        result = new NewsUI().SetProperties(action, listID);
                    }
                    else if (type.ToLower() == "product")
                    {
                        result = new ProductUI().SetProperties(action, listID);
                    }
                    else if (type.ToLower() == "functionpage")
                    {

                    }

                    if (result > 0)
                    {
                        //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                        lbl.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    }
                    else
                    {
                        //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                        lbl.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                    }
                }
                catch (Exception ex)
                {
                    lbl.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
        }
    }
}
